from 第三章.node类 import Node


class OrderList:
    def __init__(self):
        self.head = None

    def is_empty(self):
        return self.head is None

    def length(self):
        current = self.head
        count = 0
        while current is not None:
            count += 1
            current = current.get_next()

        return count

    def search(self, item):
        current = self.head
        found = False
        stop = False

        while current is not None and not found and not stop:
            if current.get_data() == item:
                found = True
            else:
                if current.get_data() > item:
                    stop = True
                else:
                    current = current.get_next()

        return found

    def index(self, item):
        if self.search(item):
            current = self.head
            found = False
            count = 0

            while not found:
                if current.get_data() == item:
                   return count
                else:
                    current = current.get_next()

                count += 1

        else:
            return '没有你要找的值'

    def add(self, item):
        current = self.head
        previous = None
        stop = False
        while current is not None and not stop:
            if current.get_data() > item:
                stop = True
            else:
                previous = current
                current = current.get_next()

        temp = Node(item)
        if previous is None:
            temp.set_next(self.head)
            self.head = temp
        else:
            temp.set_next(current)
            previous.set_next(temp)


if __name__ == "__main__":
    my_list = OrderList()
    my_list.add(1)
    my_list.add(10)
    my_list.add(7)
    print(my_list.length())
    print(my_list.search(1))
    print(my_list.index(1))